<?php
namespace app\user\controller;
use think\Db;
use mlsmt\Cabinet\Send;
class Getseller extends Common
{
    /**
     *根据经纬度获取附近的商家
     *
     */
    public function index()
    {
    	$oid = 1;
    	$longitude = input('param.longitude');
    	$latitude = input('param.latitude');
        $search = input('param.search');

        if($search){
            $list = Db::name('seller')
            ->field('id,name,area,phone,addr,longitude,latitude,shopstart,shopend,averageprice')
            //->where('oid',$oid)
            ->where('status','1')
            ->where('name|addr|phone','like','%'.$search.'%')
            ->where('is_delete',1)
            ->select();
        }else{
            $operator = Db::name('operator')->field('distance')->where('id',$oid)->find();
            isset($operator['distance'])?$distance=$operator['distance']:$distance=5;
            $distance = $distance*0.009;

            $time = (int)(date("Hi"));
            $list = Db::name('seller')
            ->field('id,name,area,addr,phone,longitude,latitude,shopstart,shopend,averageprice')
            //->where('oid',$oid)
            ->where('status','1')
            ->where('`longitude` < '.($longitude+$distance).' AND `longitude` > '.($longitude-$distance))
            ->where('`latitude` < '.($latitude+$distance).' AND `latitude` > '.($latitude-$distance))
            ->where('`shopstart` <= '.$time.' AND `shopend` >= '.$time)
            ->where('is_delete',1)
            ->select();
        }
    	
    	
		return $list;

    }
    //商家详情
    public function info()
    {
    	$oid = 1;
        //$oid = input('param.oid');
    	$id = input('param.id');
    	//获取商家详情
    	$info = Db::name('seller')
    		->where('id',$id)
    		->field('id,name,area,addr,picture,phone,longitude,latitude,shopstart,shopend,averageprice')
    		->find();
    	
        if($info['picture']){
            $info['picture'] = config('resourceurl').$info['picture'];
        }
		return $info;

    }
    //库存数量
    /**
     *获取库存数量
     *
     */
    public function stock()
    {
        $sid = input('param.sid');
        $oid = 1;
        $openid = mwdecrypt(input('param.openid'));
        $charecabinet = Db::name('charecabinet')
            ->where('sid',$sid)
            ->where('status',1)
            ->field('id,cabinet_id,devicenum')
            ->where('is_delete',1)
            ->select();

        $info['devicenum'] = 0;
        $info['stock'] = 0;
        if($charecabinet){
            foreach ($charecabinet as $key => $value) {
                $Send = new Send($value['cabinet_id']);
                $res = $Send->PowerNu();
                if($res['types']==1){
                    /*$logdata = array(
                        'oid' => $oid,
                        'mode' => 1,
                        'operation' => 'StockNum',
                        'info' => '',
                        'result' => json_encode($res),
                        'openid'  => $openid,
                    );
                    $Send->Log($logdata);//记录机柜日志*/
                    $info['devicenum'] += $value['devicenum'];
                    $info['stock'] += $res['info'];
                }
            }
        }

        $info['surplus'] =$info['devicenum']-$info['stock'];
        return $info;
    }
}
